class_name Player extends Character

## 玩家对应id
@export_enum("player1", "player2") var playerId : String
@export var equip_range : Area2D # 用来作拾取装备的检测范围
var back_dir:Vector2
var nearby_eqm: Array[Equipment] # 周围的装备
var face_right : bool = true
var ini_scale : Vector2
var weapon_scene:Weapon = null
var xuanwo_dir:Vector2
func _ready():
	ini_scale = $Sprite2D.scale
	if playerId == "player1":
		await get_tree().physics_frame
		var shovel = preload("res://scenes/Equipment/shovel.tscn").instantiate()
		$Hand.add_child(shovel)
		equip(shovel)
	else:
		await get_tree().physics_frame
		var weapon =preload("res://scenes/Weapon/weapon.tscn").instantiate()
		weapon_scene = weapon
		$Hand.add_child(weapon)
func re_send():
	$FSM/WALK.hold_cha = false
	#if state_machine:
		#state_machine.travel_to("sea_move")		
func _process(delta):
	if face_right:
		$Sprite2D.scale.x = ini_scale.x
		$Hand.scale.x = 1
	else:
		$Sprite2D.scale.x = -ini_scale.x
		$Hand.scale.x = -1

func 晕了():
	$FSM.change_state(2)

func back(p_dir):
	back_dir = p_dir
	$FSM.change_state(1)

func equip(target: Equipment):
	var sub_view_shape_parent:SubViewShapeParent
	if playerId == "player1":
		sub_view_shape_parent = CellManager.player_0_shape
	else:
		sub_view_shape_parent = CellManager.player_1_shape
	sub_view_shape_parent.add_equip(target)
	equipment = target
	equipment.player = self
	equipment.anim_player.play("used")
	equipment.position = Vector2.ZERO


func _input(event):
	
	if Input.is_action_just_released(playerId + "_equip"):
		if nearby_eqm.size():
			equip(nearby_eqm.front())
	
	#if event is InputEventKey && event.is_released():
		#if event.keycode == KEY_E:
			#print("to sea move")
			#state_machine.travel_to("sea_move")

func _on_equip_range_area_entered(body):
	#if body is Equipment:
	#	nearby_eqm.push_back(body)
	pass

func _on_equip_range_area_exited(body):
	#if body is Equipment:
	#	nearby_eqm.erase(body)
	pass
